<?php

namespace app\rongcui\controller;

use app\rongcui\model\RongcuiBuyerModel;
use app\rongcui\model\RongcuiPotentialCustomerModel;
use app\rongcui\model\RongcuiFilesModel;
use app\rongcui\model\RongcuiProjectChangeModel;
use app\rongcui\model\RongcuiProjectModel;
use app\rongcui\model\RongcuiProjectUsersModel;
use cmf\controller\AdminBaseController;

class ProjectController extends AdminBaseController
{
    function index()
    {
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['project_users.uid'] = ['in', $users];
        } else {
            $where['project_users.uid'] = ['neq', 0];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname|project.name'] = ['like', "%$keyword%"];
        }

        $rongcuiProjectModel = new RongcuiProjectModel();
        $lists = $rongcuiProjectModel
            ->alias('project')
            ->join('__USER__ user','project.mid = user.id')
            ->join('__RONGCUI_PROJECT_USERS__ project_users','project_users.pid = project.id','left')
            ->join('__RONGCUI_PROJECT_USERS__ project_user','project_user.pid = project.id and project_user.uid = '.cmf_get_current_admin_id(),'left')
            ->where($where)
            ->group('project.id')
            ->field('project.*,user.user_nickname,project_user.type as utype')
            ->order('project.id desc')
            ->paginate();

        $lists->appends(input());

        $this->assign('lists', $lists);
        $this->assign('uid', $uid);
        $this->assign('page', $lists->render());
        return $this->fetch();
    }

    function add()
    {
        $type = $this->request->param('type',0,'intval');
        if($this->request->isPost()) {
            $data = [];
            $pid = $this->request->post('pid',0,'intval');
            $data['type'] = $this->request->post('type',0,'intval');
            $data['name'] = $this->request->post('name','','trim');
            $otherser = $this->request->post('otherser',0,'intval');
            if(!$otherser) {
                $this->error('数据不完整！');
            }
            if($data['type'] == 1) {
                $k = 0;
                $data['pid'] = $pid;
                $rongcuiPotentialCustomerModel = new RongcuiPotentialCustomerModel();
                $rongcuiPotentialCustomerModel->save([
                    'abutment' => 1
                ],['id' => $pid]);
            }else{
                $k = 2;
                $data['bid'] = $pid;
                $rongcuiBuyerModel = new RongcuiBuyerModel();
                $rongcuiBuyerModel->save([
                    'abutment' => 1
                ],['id' => $pid]);
            }
            $data['oid'] = $otherser;
            $data['mid'] = cmf_get_current_admin_id();
            $rongcuiProjectModel = new RongcuiProjectModel();
            $rongcuiProjectModel->save($data);
            $pid = $rongcuiProjectModel->id;

            $users = [[
                'uid' => cmf_get_current_admin_id(),
                'type' => 1 + $k,
                'pid' => $pid
            ]];
            $firaides = $this->request->post('firaides','','trim');
            $firaides = explode(',',$firaides);
            foreach ($firaides as $firaide) {
                if(!$firaide) continue;
                $users[] = [
                    'uid' => $firaide,
                    'type' => 2 + $k,
                    'pid' => $pid
                ];
            }

            $users[] = [
                'uid' => $otherser,
                'type' => 3 - $k,
                'pid' => $pid
            ];

            $finances = $this->request->post('finances','','trim');
            $finances = explode(',',$finances);
            foreach ($finances as $finance) {
                $users[] = [
                    'uid' => $finance,
                    'type' => 5,
                    'pid' => $pid
                ];
            }

            $riskmanagements = $this->request->post('riskmanagements','','trim');
            $riskmanagements = explode(',',$riskmanagements);
            foreach ($riskmanagements as $riskmanagement) {
                $users[] = [
                    'uid' => $riskmanagement,
                    'type' => 6,
                    'pid' => $pid
                ];
            }

            if($users) {
                $rongcuiProjectUsersModel = new RongcuiProjectUsersModel();
                $rongcuiProjectUsersModel->saveAll($users);
            }
            setLog($pid,1,0,cmf_get_current_admin_id(),'发起立项');
            $this->success('立项成功！',url('index'));
        }
        $this->assign('type',$type);
        return $this->fetch();
    }

    public function edit() {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $result = $rongcuiProjectModel
            ->alias('project')
            ->where([
                'project.id' => $id
            ])
            ->find();
        if($result['status'] != 1) {
//            $this->error('改状态下禁止修改！');
        }
        if($this->request->isPost()) {
            $arrData = $this->request->post();
            $data = [];
            if($arrData['name'] != $result['name']) {
                $data['name'] = $arrData['name'];
            }
            $otherser = $this->request->post('otherser',0,'intval');
            if(!$otherser) {
                $this->error('数据不完整！');
            }
            if($otherser != $result['oid']) {
                $data['oid'] = $otherser;
            }
            if($data) {
                $rongcuiProjectModel->save($data,[
                    'id' => $id
                ]);
            }
            if($result['type'] == 1) {
                $k = 0;
            }else {
                $k = 2;
            }
            $users = [[
                'uid' => $result['mid'],
                'type' => 1 + $k,
                'pid' => $id
            ]];
            $firaides = $this->request->post('firaides','','trim');
            $firaides = explode(',',$firaides);
            foreach ($firaides as $firaide) {
                if(!$firaide) continue;
                $users[] = [
                    'uid' => $firaide,
                    'type' => 2 + $k,
                    'pid' => $id
                ];
            }

            $users[] = [
                'uid' => $otherser,
                'type' => 3 - $k,
                'pid' => $id
            ];

            $finances = $this->request->post('finances','','trim');
            $finances = explode(',',$finances);
            foreach ($finances as $finance) {
                $users[] = [
                    'uid' => $finance,
                    'type' => 5,
                    'pid' => $id
                ];
            }

            $riskmanagements = $this->request->post('riskmanagements','','trim');
            $riskmanagements = explode(',',$riskmanagements);
            foreach ($riskmanagements as $riskmanagement) {
                $users[] = [
                    'uid' => $riskmanagement,
                    'type' => 6,
                    'pid' => $id
                ];
            }

            if($users) {
                $rongcuiProjectUsersModel = new RongcuiProjectUsersModel();
                $rongcuiProjectUsersModel
                    ->where([
                        'pid' => $id
                    ])
                    ->delete();
                $rongcuiProjectUsersModel->saveAll($users);
            }
            setLog($id,$result['status'],$result['status'],cmf_get_current_admin_id(),'项目编辑');
            $this->success('保存成功！',url('index'));
        }

        $users = db('rongcui_project_users')
            ->alias('puser')
            ->join('__USER__ user','puser.uid = user.id')
            ->where([
                'pid' => $id
            ])
            ->field('group_concat(user.id) ids,group_concat(user_nickname) names,puser.type')
            ->order('type')
            ->group('puser.type')
            ->select();

        foreach ($users as $user) {
            switch ($user['type']) {
                case 2:
                case 4:
                    $result['ouser'] = $user;
                    break;
                case 5:
                    $result['cwuser'] = $user;
                    break;
                case 6:
                    $result['fkuser'] = $user;
                    break;
                case 1:
                    if($result['type'] == 2) {
                        $result['puser'] = $user;
                    }
                    break;
                case 3:
                    if($result['type'] == 1) {
                        $result['puser'] = $user;
                    }
                    break;
            }
        }

        if($result['pid']) {
            $result['info'] = db('rongcui_potential_customer')->field('company name,id')->find($result['pid']);
        }
        if($result['bid']) {
            $result['info'] = db('rongcui_buyer')->field('name,id')->find($result['bid']);
        }

        $this->assign('result',$result);
        return $this->fetch();
    }

    function team() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $info = $rongcuiProjectModel
            ->alias('project')
            ->join('__USER__ user','project.mid = user.id')
            ->where([
                'project.id' => $pid
            ])
            ->field('project.*,user.user_nickname')
            ->find();
        if($info['pid']) {
            $info['product'] = db('rongcui_potential_customer')->find($info['pid']);
        }
        if($info['bid']) {
            $info['buyer'] = db('rongcui_buyer')->find($info['bid']);
        }

        $rongcuiProjectUsersModel = new RongcuiProjectUsersModel();
        $users = $rongcuiProjectUsersModel
            ->alias('project_users')
            ->join('__USER__ user','project_users.uid = user.id')
            ->where([
                'pid' => $pid
            ])
            ->field('project_users.*,user.user_nickname')
            ->order('type')
            ->select();

        $logs = db('rongcui_project_log')
            ->alias('log')
            ->join('__USER__ user','log.uid = user.id')
            ->where([
                'pid' => $pid
            ])
            ->field('log.*,user.user_nickname')
            ->select();

        $this->assign('users',$users);
        $this->assign('info',$info);
        $this->assign('logs',$logs);
        return $this->fetch();
    }

    function confirm() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $info = $rongcuiProjectModel
            ->alias('project')
            ->join('__USER__ user','project.mid = user.id')
            ->where([
                'project.id' => $pid,
                'project.status' => 1,
            ])
            ->field('project.*,user.user_nickname')
            ->find();
        if(!$info) {
            $this->error('访问错误！');
        }
        if($this->request->isPost()) {
            $oid = $this->request->param('oid',0,'intval');
            if(!$oid) {
                $this->error('参数错误！');
            }
            if($info['type'] == 1) {
                $rongcuiProjectModel->save([
                    'bid' => $oid,
                    'status' => 2
                ],['id'=>$pid,'status' => 1]);
                $type = 4;
            }else{
                $rongcuiProjectModel->save([
                    'pid' => $oid,
                    'status' => 2
                ],['id'=>$pid,'status' => 1]);
                $type = 2;
            }

            $users = [];
            $firaides = $this->request->post('firaides','','trim');
            $firaides = explode(',',$firaides);
            foreach ($firaides as $firaide) {
                if(!$firaide)continue;
                $users[] = [
                    'uid' => $firaide,
                    'type' => $type,
                    'pid' => $pid
                ];
            }

            if($users) {
                $rongcuiProjectUsersModel = new RongcuiProjectUsersModel();
                $rongcuiProjectUsersModel->saveAll($users);
            }
            setLog($pid,2,1,cmf_get_current_admin_id(),'项目确认');
            $this->success('确认完成！',url('index'));
        }

        if($info['type'] == 1) {
            $rongcuiPotentialCustomerModel = new RongcuiPotentialCustomerModel();
            $data = $rongcuiPotentialCustomerModel->find($info['pid']);
            $this->assign('name',$data['company']);
        }else{
            $rongcuiBuyerModel = new RongcuiBuyerModel();
            $data = $rongcuiBuyerModel->find($info['bid']);
            $this->assign('name',$data['name']);
        }

        $rongcuiProjectUsersModel = new RongcuiProjectUsersModel();
        $users = $rongcuiProjectUsersModel
            ->alias('project_users')
            ->join('__USER__ user','project_users.uid = user.id')
            ->where([
                'pid' => $pid
            ])
            ->field('project_users.*,group_concat(user.user_nickname) as teamname')
            ->group('project_users.type')
            ->order('type')
            ->select();

        $this->assign('users',$users);
        $this->assign('info',$info);
        $this->assign('pid',$pid);
        return $this->fetch();
    }

    function dataupdate() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }

        $rongcuiProjectModel = new RongcuiProjectModel();
        $project = $rongcuiProjectModel->where([
            'id' => $pid
        ])->find();
        if(!$project) {
            $this->error('访问错误！');
        }
        $rongcuiFilesModel = new RongcuiFilesModel();
        if($this->request->isPost()) {
            $arrData = $this->request->post();
            $files = [];
            $time = time();
            if($project['status']<11) {
                $type = 3;
                $status = 3;
                $msg = '申请立项';
            }elseif($project['status']<14){
                $type = 6;
                $status = 12;
                $msg = '凭证审核';
            }else{
                $type = 7;
                $status = 15;
                $msg = '申请完成';
            }
            if(isset($arrData['file_urls'])) {
                foreach ($arrData['file_urls'] as $key => $url) {
                    $files[] = [
                        'file_name' => $arrData['file_names'][$key],
                        'file' => $url,
                        'uid' => cmf_get_current_admin_id(),
                        'pid' => $arrData['pid'],
                        'type' => $type,
                        'create_time' => $time,
                    ];
                }
                if($files) {
                    $rongcuiFilesModel
                        ->allowField(true)
                        ->saveAll($files);
                }
            }
            $data['status'] = $status;
            if($project['check_cw'] == 2) $data['check_cw'] = 0;
            if($project['check_fk'] == 2) $data['check_fk'] = 0;
            $rongcuiProjectModel->save($data,[
                'id' => $pid
            ]);
            setLog($pid,$status,$project['status'],cmf_get_current_admin_id(),$msg);
            $this->success('资料提交成功！',url('index'));
        }

        $files = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0
            ])
            ->field('files.*,user.user_nickname')
            ->order('create_time,id')
            ->select();

        $this->assign('files',$files);
        $this->assign('project',$project);
        return $this->fetch();
    }

    function check_cw() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $project = $rongcuiProjectModel->where([
            'id' => $pid
        ])->find();
        if(!$project) {
            $this->error('访问错误！');
        }

        if($this->request->isPost()) {
            $check = $this->request->post('check',0,'intval');
            $flag = 0;
            if($project['status'] < 5) {
                $status = 3;
            }elseif($project['status'] < 9){
                $status = 6;
                $flag = 3;
            }elseif($project['status'] < 12){
                $status = 9;
                $flag = 6;
            }elseif($project['status'] < 15){
                $status = 12;
                $flag = 9;
            }else{
                $status = 15;
                $flag = 12;
            }
            if($check) {
                if($project['check_fk'] == 1) {
                    $rongcuiProjectModel->save([
                        'check_fk' => 0,
                        'status' => 5 + $flag
                    ],[
                        'id' => $pid,
                        'status' => $status,
                        'check_cw' => 0
                    ]);
                    if($status == 15) {
                        $this->project_final($project);
                    }
                }else{
                    if($status == 12) {
                        $rongcuiProjectModel->save([
                            'check_cw' => 0,
                            'status' => 5 + $flag
                        ],[
                            'id' => $pid,
                            'status' => $status,
                            'check_cw' => 0
                        ]);
                    }else{
                        $rongcuiProjectModel->where([
                            'id' => $pid,
                            'status' => $status,
                            'check_cw' => 0
                        ])->setField('check_cw',1);
                    }
                }
            }else{
                $rongcuiProjectModel->save([
                    'check_cw' => 2,
                    'status' => 4 + $flag
                ],[
                    'id' => $pid,
                    'status' => $status,
                    'check_cw' => 0
                ]);
            }
            if($check) {
                if($project['check_fk'] == 1){
                    setLog($pid,5 + $flag,$project['status'],cmf_get_current_admin_id(),'财务审核成功!');
                }else{
                    setLog($pid,$status,$project['status'],cmf_get_current_admin_id(),'财务审核成功!');
                }
            }else{
                setLog($pid,4 + $flag,$project['status'],cmf_get_current_admin_id(),'财务审核失败!');
            }
            $this->success('审核成功！',url('index'));
        }

        $rongcuiFilesModel = new RongcuiFilesModel();
        $files = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0
            ])
            ->field('files.*,user.user_nickname')
            ->order('create_time,id')
            ->select();

        $this->assign('files',$files);
        $this->assign('project',$project);

        return $this->fetch();
    }

    function check_fk() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $project = $rongcuiProjectModel->where([
            'id' => $pid
        ])->find();
        if(!$project) {
            $this->error('访问错误！');
        }

        if($this->request->isPost()) {
            $check = $this->request->post('check',0,'intval');
            $flag = 0;
            if($project['status'] < 5) {
                $status = 3;
            }elseif($project['status'] < 9){
                $status = 6;
                $flag = 3;
            }elseif($project['status'] < 12){
                $status = 9;
                $flag = 6;
            }else{
                $status = 15;
                $flag = 12;
            }
            if($check) {
                if($project['check_cw'] == 1) {
                    $rongcuiProjectModel->save([
                        'check_cw' => 0,
                        'status' => 5 + $flag
                    ],[
                        'id' => $pid,
                        'status' => $status,
                        'check_fk' => 0
                    ]);
                    if($status == 15) {
                        $this->project_final($project);
                    }
                }else{
                    $rongcuiProjectModel->where([
                        'id' => $pid,
                        'status' => $status,
                        'check_fk' => 0
                    ])->setField('check_fk',1);
                }
            }else{
                $rongcuiProjectModel->save([
                    'check_fk' => 2,
                    'status' => 4 + $flag
                ],[
                    'id' => $pid,
                    'status' => $status,
                    'check_fk' => 0
                ]);
            }
            if($check) {
                if($project['check_cw'] == 1){
                    setLog($pid,5 + $flag,$project['status'],cmf_get_current_admin_id(),'风控审核成功!');
                }else{
                    setLog($pid,$status,$project['status'],cmf_get_current_admin_id(),'风控审核成功!');
                }
            }else{
                setLog($pid,4 + $flag,$project['status'],cmf_get_current_admin_id(),'风控审核失败!');
            }
            $this->success('审核成功！',url('index'));
        }

        $rongcuiFilesModel = new RongcuiFilesModel();
        $files = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0
            ])
            ->field('files.*,user.user_nickname')
            ->order('create_time,id')
            ->select();

        $this->assign('files',$files);
        $this->assign('project',$project);

        return $this->fetch();
    }

    function change() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $project = $rongcuiProjectModel->where([
            'id' => $pid
        ])->find();
        if(!$project) {
            $this->error('访问错误！');
        }
        $rongcuiFilesModel = new RongcuiFilesModel();
        $rongcuiProjectChangeModel = new RongcuiProjectChangeModel();

        if($this->request->isPost()) {
            $arrData = $this->request->post();
            $data = [];
            $time = time();
            foreach ($arrData['name'] as $key => $name) {
                $data[] = [
                    'pid' => $pid,
                    'uid' => cmf_get_current_admin_id(),
                    'name' => $name,
//                    'value' => $arrData['value'][$key],
                    'date' => strtotime($arrData['date'][$key]),
                    'create_time' => $time,
                ];
            }
            if(isset($arrData['file_urls'])){
                $files = [];
                foreach ($arrData['file_urls'] as $key => $url) {
                    $files[] = [
                        'file_name' => $arrData['file_names'][$key],
                        'file' => $url,
                        'uid' => cmf_get_current_admin_id(),
                        'pid' => $arrData['pid'],
                        'type' => 5,
                        'create_time' => $time,
                    ];
                }
                if($files) {
                    $rongcuiFilesModel
                        ->allowField(true)
                        ->saveAll($files);
                }
            }
            if($data) {
                $rongcuiProjectChangeModel
                    ->where([
                        'pid' => $pid
                    ])
                    ->delete();
                $flag = $rongcuiProjectChangeModel->allowField(true)->saveAll($data);
                if($flag) {
                    $data['status'] = 9;
                    if($project['check_cw'] == 2) $data['check_cw'] = 0;
                    if($project['check_fk'] == 2) $data['check_fk'] = 0;
                    $rongcuiProjectModel->allowField(true)->save($data,[
                        'id' => $pid
                    ]);
                    setLog($pid,9,$project['status'],cmf_get_current_admin_id(),'申请变更');
                    $this->success('提交成功！',url('index'));
                }
            }
            $this->error('请填写资料！');
        }
        $lists = $rongcuiProjectChangeModel
            ->where([
                'pid' => $pid
            ])
            ->select();

        $files = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0
            ])
            ->field('files.*,user.user_nickname')
            ->order('create_time,id')
            ->select();

        $this->assign('files',$files);
        $this->assign('project',$project);
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    function finalized() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }

        $rongcuiProjectModel = new RongcuiProjectModel();
        $project = $rongcuiProjectModel->where([
            'id' => $pid,
            'status' => ['in','5,7']
        ])->find();
        if(!$project) {
            $this->error('访问错误！');
        }
        $rongcuiFilesModel = new RongcuiFilesModel();
        if($this->request->isPost()) {
            $arrData = $this->request->post();
            $files = [];
            $time = time();
            if(isset($arrData['file_urls'])) {
                foreach ($arrData['file_urls'] as $key => $url) {
                    $files[] = [
                        'file_name' => $arrData['file_names'][$key],
                        'file' => $url,
                        'uid' => cmf_get_current_admin_id(),
                        'pid' => $pid,
                        'type' => 4,
                        'create_time' => $time,
                    ];
                }
                if($files) {
                    $rongcuiFilesModel
                        ->allowField(true)
                        ->saveAll($files);
                }
            }
            $data = [
                'status' => 6,
            ];
            if($project['check_cw'] == 2) $data['check_cw'] = 0;
            if($project['check_fk'] == 2) $data['check_fk'] = 0;
            $yunying = $this->request->post('yunying',0,'intval');
            $rongcuiProjectUsersModel = new RongcuiProjectUsersModel();
            if($yunying) {
                if($project['yunying'] != $yunying){
                    $data['yunying'] = $yunying;
                    if($project['yunying']) {
                        $rongcuiProjectUsersModel->save([
                            'uid' => $yunying,
                            'type' => 7,
                            'pid' => $pid,
                        ],[
                            'type' => 7,
                            'pid' => $pid,
                        ]);
                    }else{
                        $rongcuiProjectUsersModel->save([
                            'uid' => $yunying,
                            'type' => 7,
                            'pid' => $pid,
                        ]);
                    }
                }
            }else{
                if($project['yunying']){
                    $data['yunying'] = 0;
                    $rongcuiProjectUsersModel->where([
                        'type' => 7,
                        'pid' => $pid,
                    ])->delete();
                }
            }
            $rongcuiProjectModel
                ->save($data,[
                    'id' => $pid
                ]);
            setLog($pid,$data['status'],$project['status'],cmf_get_current_admin_id(),'申请落定');
            $this->success('资料提交成功！',url('index'));
        }

        $files = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0
            ])
            ->field('files.*,user.user_nickname')
            ->order('create_time,id')
            ->select();

        if($project['yunying']) {
            $project['yunyinguser'] = db('user')->find($project['yunying']);
        }

        $this->assign('files',$files);
        $this->assign('project',$project);
        return $this->fetch();
    }

    function changestatus() {
        $pid = $this->request->param('pid',0,'intval');
        $rongcuiProjectChangeModel = new RongcuiProjectChangeModel();
        $lists = $rongcuiProjectChangeModel
            ->where([
                'pid' => $pid
            ])
            ->order('date,id')
            ->select();

        $rongcuiProjectModel = new RongcuiProjectModel();
        $info = $rongcuiProjectModel->find($pid);

        $this->assign('uid',cmf_get_current_admin_id());
        $this->assign('info',$info);
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    function changetofinal() {
        $id = $this->request->param('id',0,'intval');
        $rongcuiProjectChangeModel = new RongcuiProjectChangeModel();
        $flag = $rongcuiProjectChangeModel->allowField(true)->save([
            'final_time' => time(),
            'status' => 1
        ],[
            'id' => $id
        ]);
        if($flag !== false) {
            $this->success('操作成功！');
        }else{
            $this->error('操作失败！');
        }
    }

//    function final() {
//        $pid = $this->request->param('pid',0,'intval');
//        $rongcuiProjectModel = new RongcuiProjectModel();
//        $project = $rongcuiProjectModel->where([
//            'id' => $pid,
//        ])->find();
//        if(!$project) {
//            $this->error('访问错误！');
//        }
////        $rongcuiProjectModel->save([
////            'status' => 13
////        ],['id' => $pid]);
////        setLog($pid,13,$project['status'],cmf_get_current_admin_id(),'项目完成');
////        $this->success('操作成功！');
//        return $this->fetch();
//    }

    function files() {
        $pid = $this->request->param('pid',0,'intval');
        $rongcuiFilesModel = new RongcuiFilesModel();
        $lists = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0,
                'type' => ['in','3,4,5,6,7'],
            ])
            ->field('files.*,user.user_nickname')
            ->select();

        $this->assign('lists',$lists);
        return $this->fetch();
    }

    function change_sh() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $project = $rongcuiProjectModel->where([
            'id' => $pid,
        ])->find();
        if(!$project) {
            $this->error('访问错误！');
        }

        if($this->request->isPost()) {
            $check = $this->request->post('check',0,'intval');
            if($check) {
                $rongcuiProjectModel->where([
                    'id' => $pid
                ])->setField('status',11);
                setLog($pid,11,$project['status'],cmf_get_current_admin_id(),'负责人审核成功!');
            }else{
                $rongcuiProjectModel->where([
                    'id' => $pid
                ])->setField('status',10);
                setLog($pid,10,$project['status'],cmf_get_current_admin_id(),'负责人审核失败!');
            }
            $this->success('审核成功！',url('index'));
        }

        $rongcuiFilesModel = new RongcuiFilesModel();
        $files = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0
            ])
            ->field('files.*,user.user_nickname')
            ->order('create_time,id')
            ->select();

        $this->assign('files',$files);
        $this->assign('project',$project);
        return $this->fetch();
    }

    private function project_final($project) {
        $rongcuiBuyerModel = new RongcuiBuyerModel();
        $rongcuiBuyerModel->where([
            'id' => $project['bid']
        ])->setField('abutment',2);

        $rongcuiPotentialCustomerModel = new RongcuiPotentialCustomerModel();
        $rongcuiPotentialCustomerModel->where([
            'id' => $project['pid']
        ])->setField('abutment',2);

        return true;
    }

    public function set_yunying() {
        $pid = $this->request->param('pid',0,'intval');
        if(!$pid) {
            $this->error('参数错误！');
        }
        $rongcuiProjectModel = new RongcuiProjectModel();
        $project = $rongcuiProjectModel->where([
            'id' => $pid,
        ])->find();
        if(!$project) {
            $this->error('访问错误！');
        }

        if($this->request->isPost()) {
            $yunying = $this->request->post('yunying',0,'intval');
            if($yunying) {
                $rongcuiProjectUsersModel = new RongcuiProjectUsersModel();
                $rongcuiProjectUsersModel->save([
                    'uid' => $yunying,
                    'type' => 7,
                    'pid' => $pid,
                ]);
                $rongcuiProjectModel
                    ->where([
                        'id' => $pid
                    ])
                    ->setField('yunying',$yunying);
                setLog($pid,$project['status'],$project['status'],cmf_get_current_admin_id(),'分配运营人员！');
                $this->success('分配运营人员！',url('index'));
            }else{
                $this->error('请选择运营人员！');
            }
        }

        $rongcuiFilesModel = new RongcuiFilesModel();
        $files = $rongcuiFilesModel
            ->alias('files')
            ->join('__USER__ user','files.uid = user.id')
            ->where([
                'pid' => $pid,
                'is_del' => 0
            ])
            ->field('files.*,user.user_nickname')
            ->order('create_time,id')
            ->select();

        $this->assign('files',$files);
        $this->assign('project',$project);
        return $this->fetch();
    }
}